package com.ag.webservice;


import java.net.MalformedURLException;

import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.junit.Test;

import com.dcampus.user.webservice.I_Group;
import com.dcampus.user.webservice.I_ObjectService;
import com.dcampus.user.webservice.I_Position;
import com.dcampus.user.webservice.I_Role;
import com.dcampus.user.webservice.I_User;


public class ObjectServiceUnitTest {
	
	private static I_ObjectService service = null;
    static String ROLE_ID = "admin";
    static String SYSTEM_ID = "Student Management System";//ssm.getSubSystems()[0].getId();
    static String CATEGORY_ID = "user-manage";
    static String FUNCTION_ID = "view-user";

	static {
		Service serviceModel = new ObjectServiceFactory().create(I_ObjectService.class);

		String serviceURL = "http://cas.gzfda.gov.cn/services/UserObjectService";
		//String serviceURL = "http://heaven.net/services/UserObjectService";

		try {
			service = (I_ObjectService) new XFireProxyFactory().create(serviceModel, serviceURL);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	@Test 
	public void testGetUser() throws Exception{
		I_User user = service.getUser("sa");
		System.out.println(user.getId());
		System.out.println(user.getName());
		System.out.println(user.getDescription());
		System.out.println(user.getGender());
		System.out.println(user.getIdCard());
		System.out.println(user.getCode());
		System.out.println(user.getQq());
		System.out.println(user.getMsn());
		System.out.println(user.getMail());
		System.out.println(user.getOfficeNumber());
		System.out.println(user.getHomeNumber());
		System.out.println(user.getPhoneNumber());
		System.out.println(user.getMobilePhoneNumber());
		System.out.println(user.getAddress());
		System.out.println(user.getZipCode());
		for(String att : user.getRoles()){
			System.out.println("roles: " + att);
		}
		for(String att : user.getGroups()){
			System.out.println("groups: " + att);
		}
		for(String att : user.getPositions()){
			System.out.println("positions: " + att);
		}
	}

    @Test 
    public void testGetUserByCertificateId() throws Exception{
        I_User user = service.getUserByCertificateId("21");
        System.out.println(user.getId());
        System.out.println(user.getCertificateId());
        System.out.println(user.getName());
        System.out.println(user.getDescription());
        System.out.println(user.getGender());
        System.out.println(user.getIdCard());
        System.out.println(user.getCode());
        System.out.println(user.getMail());
        System.out.println(user.getPhoneNumber());
        System.out.println(user.getMobilePhoneNumber());
        System.out.println(user.getAddress());
        System.out.println(user.getZipCode());
        for(String att : user.getRoles()){
            System.out.println("roles: " + att);
        }
        for(String att : user.getGroups()){
            System.out.println("groups: " + att);
        }
        for(String att : user.getPositions()){
            System.out.println("positions: " + att);
        }
    }

    
    @Test 
    public void testGetAllUsers() throws Exception{
        I_User[] users = service.getAllUsers();
        System.out.println("users.length:" + users.length);
        System.out.println("users[0]:" + users[0].getName());
    }
    
    @Test
    public void testGetUsersPaging()throws Exception{
        System.out.println("allUsersCount:" + service.getAllUsersCount());
        System.out.println("(0,2)" + service.getUsersPaging(0, 2).length);
        System.out.println("(-1,2)" + service.getUsersPaging(-1, 2).length);
        System.out.println("(3,2)" + service.getUsersPaging(3, 2).length);
        System.out.println("(3,11)" + service.getUsersPaging(3, 11).length);
        System.out.println("(3,100)" + service.getUsersPaging(3, 100).length);
        System.out.println("(300,400)" + service.getUsersPaging(300, 400).length);
    }
    
	@Test 
	public void testGetUserOfRole() throws Exception{
		I_User[] users = service.getUsersOfRole("manager");
		System.out.println(users.length);
		for(I_User user:users){
			System.out.println(user.getId() + " : " + user.getName());
		}
	}
	
	@Test 
	public void testGetUsersOfPosition() throws Exception{
		I_User[] users = service.getUsersOfPosition("0001");
		System.out.println(users.length);
		for(I_User user:users){
			System.out.println(user.getId() + " : " + user.getName());
		}
	}
	
	@Test 
	public void testGetUsersOfGroup() throws Exception{
		I_User[] users = service.getUsersOfGroup("001001");
		System.out.println(users.length);
		for(I_User user:users){
			System.out.println(user.getId() + " : " + user.getName());
		}
	}
	
	@Test 
	public void testGetRoles() throws Exception{
		I_Role[] roles = service.getRoles("sa");
		System.out.println(roles.length);
		for(I_Role role:roles){
			System.out.println(role.getId() + " : " + role.getName());
		}
	}
	
	@Test 
	public void testGetPositions() throws Exception{
		I_Position[] positions = service.getPositions("sa");
		System.out.println(positions.length);
		for(I_Position position:positions){
			System.out.println(position.getId() + " : " + position.getName());
		}
	}
	
	@Test 
	public void testGetRoleOfId() throws Exception{
		I_Role role = service.getRoleOfId("manager");
		System.out.println(role.getId() + " : " + role.getName());
	}
	
	@Test 
	public void testGetPositionOfId() throws Exception{
		I_Position po = service.getPositionOfId("0001");
		System.out.println(po.getId() + " : " + po.getName());
	}
	
	@Test 
	public void testGetAllRoles() throws Exception{
		I_Role[] roles = service.getAllRoles();
		System.out.println(roles.length);
		for(I_Role role:roles){
			System.out.println(role.getId() + " : " + role.getName());
		}
	}
	
	@Test 
	public void testGetAllPositions() throws Exception{
		I_Position[] positions = service.getAllPositions();
		System.out.println(positions.length);
		for(I_Position position:positions){
			System.out.println(position.getId() + " : " + position.getName());
		}
	}
	
	@Test 
	public void testGetRolesOfSubSystem() throws Exception{
		I_Role[] roles = service.getRolesOfSubSystem("Student Management System");
		System.out.println(roles.length);
		for(I_Role role:roles){
			System.out.println(role.getId() + " : " + role.getName());
		}
	}
	
	@Test 
	public void testGetGroups() throws Exception{
		I_Group[] groups = service.getGroups("sa");
		System.out.println(groups.length);
		for(I_Group group:groups){
			System.out.println(group.getId() + " : " + group.getName());
		}
	}
	
	@Test 
	public void testGetGroupOfId() throws Exception{
		I_Group group = service.getGroupOfId("001001");
		System.out.println(group.getId() + " : " + group.getName());
	}
	
	@Test 
	public void testGetAllGroups() throws Exception{
		I_Group[] groups = service.getAllGroups();
		System.out.println(groups.length);
		for(I_Group group:groups){
			System.out.println(group.getId() + " : " + group.getName());
		}
	}
	
	@Test 
	public void testGetRootGroups() throws Exception{
		I_Group[] groups = service.getRootGroups();
		System.out.println(groups.length);
		for(I_Group group:groups){
			System.out.println(group.getId() + " : " + group.getName());
		}
	}
	
	@Test 
	public void testGetSubGroups() throws Exception{
		I_Group[] groups = service.getSubGroups("001");
		System.out.println(groups.length);
		for(I_Group group:groups){
			System.out.println(group.getId() + " : " + group.getName());
		}
	}
	
	@Test
	public void testAddUserToRole()throws Exception{
	    service.addUserToRole("sa", "cyry");
	}
	
	@Test
	public void testRemoveUserFromRole()throws Exception{
	    service.removeUserFromRole("sa", "cyry");
	}
	
	@Test
	public void testUpdateUserInfo()throws Exception{
	    //service.updateUserInfo("sa", "1", "2", "3", "4", "5", "6", "7", "8", "9");
        service.updateUserInfo("sa", "11", "22", "33", "44", "55", "66", "77", "88", "99");
	}
}
